package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.utils.APPUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import rx.functions.Action1;

/**
 * Created by admin on 2016/1/14.
 */
public class WeekStartAdapter extends RecyclerView.Adapter<WeekStartAdapter.MyViewHolder>
{
    private Activity activity;
    private JSONArray list;
    private int WeekType = 2;         //请求类型：1-艺人周星榜，2-粉丝周星榜
    public WeekStartAdapter(Activity activity, JSONArray list,int WeekType){
        this.activity = activity;
        this.list = list;
        this.WeekType = WeekType;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder =  new MyViewHolder(LayoutInflater.from(
                activity).inflate(R.layout.main_ranking_itemweek, parent,
                false));

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        JSONObject user = list.optJSONObject(position);
        APPUtils.displayImage(APPUtils.createGiftImg(user.optInt("Gift_ID")), holder.imgWeekGiftAvatar);
        holder.txtGiftName.setText(user.optString("Gift_Name"));
        holder.txtGiftCount.setText(Html.fromHtml("艺人奖励保底" + user.optString("WeekStarMin") + "个"));
        JSONArray array = user.optJSONArray("Rank");

        switch (array.length()){
            case 3:
                JSONObject object3 = array.optJSONObject(2);
                holder.txtUserName3.setText(object3.optString("NickName"));
                holder.txtGiftCount3.setText(Html.fromHtml("收到" + "<font color='#f5a623'>" + object3.optString("SUM_Number") + "</font>" + "个"));
                holder.imgUserAvatar3.post(new Runnable() {
                    @Override
                    public void run() {
                        APPUtils.displayImage(APPUtils.createUserAvatar(object3.optInt("User_ID")), holder.imgUserAvatar3, activity.getBaseContext(), holder.imgUserAvatar3.getHeight() / 2);
                    }
                });
                holder.vContent3.setTag(object3);
                RxView.clicks(holder.vContent3).map(View->holder.vContent3).subscribe(OC_UserCard);
                holder.imgUserLevel3.setImageResource(APPUtils.getLevelPic(activity,object3.optInt("Level")));
            case 2:
                JSONObject object2 = array.optJSONObject(1);
                holder.txtUserName2.setText(object2.optString("NickName"));
                holder.txtGiftCount2.setText(Html.fromHtml("收到" + "<font color='#f5a623'>" + object2.optString("SUM_Number") + "</font>" + "个"));
                holder.imgUserAvatar2.post(new Runnable() {
                    @Override
                    public void run() {
                        APPUtils.displayImage(APPUtils.createUserAvatar(object2.optInt("User_ID")), holder.imgUserAvatar2, activity.getBaseContext(), holder.imgUserAvatar2.getHeight() / 2);
                    }
                });
                holder.vContent2.setTag(object2);
                RxView.clicks(holder.vContent2).map(View->holder.vContent2).subscribe(OC_UserCard);
                holder.imgUserLevel2.setImageResource(APPUtils.getLevelPic(activity, object2.optInt("Level")));
            case 1:
                JSONObject object1 = array.optJSONObject(0);

                holder.txtUserName1.setText(object1.optString("NickName"));
                holder.txtGiftCount1.setText(Html.fromHtml("收到" + "<font color='#f5a623'>" + object1.optString("SUM_Number") + "</font>" + "个"));
                holder.imgUserAvatar1.post(new Runnable() {
                    @Override
                    public void run() {
                        APPUtils.displayImage(APPUtils.createUserAvatar(object1.optInt("User_ID")), holder.imgUserAvatar1, activity.getBaseContext(), holder.imgUserAvatar1.getHeight() / 2);
                    }
                });
                holder.vContent1.setTag(object1);
                RxView.clicks(holder.vContent1).map(View->holder.vContent1).subscribe(OC_UserCard);
                holder.imgUserLevel1.setImageResource(APPUtils.getLevelPic(activity, object1.optInt("Level")));
        }
    }

    private Action1<View> OC_UserCard = v->{
        if(v.getTag()==null){
            return;
        }
        JSONObject u = (JSONObject) v.getTag();
        if(WeekType==1){
            APPUtils.Dialog_UserInfoCard_To(activity, u.optInt("User_ID"), u.optString("NickName"), -1,-1 ,u.optInt("Level"),false,false);
        }else {
            APPUtils.Dialog_UserInfoCard_To(activity, u.optInt("User_ID"), u.optString("NickName"), -1, u.optInt("Level"), -1,false,false);
        }
    };

    @Override
    public int getItemCount()
    {
        return list==null?0: list.length();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        TextView txtGiftName,txtUserName1,txtUserName2,txtUserName3;
        TextView txtGiftCount,txtGiftCount1,txtGiftCount2,txtGiftCount3;
        ImageView imgWeekGiftAvatar,imgUserAvatar1,imgUserAvatar2,imgUserAvatar3;
        ImageView imgUserLevel1,imgUserLevel2,imgUserLevel3;
        View vContent,vContent1,vContent2,vContent3;
        View vMain;

        public MyViewHolder(View view)
        {
            super(view);
            vMain = view.findViewById(R.id.vMain);
            vContent = view.findViewById(R.id.vContent);
            vContent1 = view.findViewById(R.id.vContent1);
            vContent2 = view.findViewById(R.id.vContent2);
            vContent3 = view.findViewById(R.id.vContent3);
            imgWeekGiftAvatar = (ImageView) view.findViewById(R.id.imgWeekGiftAvatar);
            imgUserAvatar1 = (ImageView) view.findViewById(R.id.imgUserAvatar1);
            imgUserAvatar2 = (ImageView) view.findViewById(R.id.imgUserAvatar2);
            imgUserAvatar3 = (ImageView) view.findViewById(R.id.imgUserAvatar3);

            imgUserLevel1 = (ImageView) view.findViewById(R.id.imgUserLevel1);
            imgUserLevel2 = (ImageView) view.findViewById(R.id.imgUserLevel2);
            imgUserLevel3 = (ImageView) view.findViewById(R.id.imgUserLevel3);

            txtGiftName = (TextView) view.findViewById(R.id.txtGiftName);
            txtUserName1 = (TextView) view.findViewById(R.id.txtUserName1);
            txtUserName2 = (TextView) view.findViewById(R.id.txtUserName2);
            txtUserName3 = (TextView) view.findViewById(R.id.txtUserName3);

            txtGiftCount = (TextView) view.findViewById(R.id.txtGiftCount);
            txtGiftCount1 = (TextView) view.findViewById(R.id.txtGiftCount1);
            txtGiftCount2 = (TextView) view.findViewById(R.id.txtGiftCount2);
            txtGiftCount3 = (TextView) view.findViewById(R.id.txtGiftCount3);
        }
    }

}
